package com.engine.common.baseEntity.gis;

import com.alibaba.fastjson.annotation.JSONField;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Component
public class BaseFeatureCollection<T extends BaseGeoEntity> implements Serializable {

    public BaseFeatureCollection(){
        baseFeatures = new ArrayList<BaseFeature<T>>();
        type = "FeatureCollection";
    }

    public BaseFeatureCollection(List<T> geoEntitys) {
        baseFeatures = new ArrayList<BaseFeature<T>>();
        for (BaseGeoEntity geoEntity : geoEntitys) {
            baseFeatures.add(new BaseFeature(geoEntity));
        }
        type = "FeatureCollection";
    }

    public void AddFeatures(List<T> geoEntitys){
        for (BaseGeoEntity geoEntity : geoEntitys) {
            baseFeatures.add(new BaseFeature(geoEntity));
        }
    }
    public void AddFeature(T geoEntity){
        baseFeatures.add(new BaseFeature(geoEntity));
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @JSONField(name = "features")
    public List<BaseFeature<T>> getBaseFeatures() {
        return baseFeatures;
    }

    public void setBaseFeatures(List<BaseFeature<T>> baseFeatures) {
        this.baseFeatures = baseFeatures;
    }

    private String type;

    @JSONField(name = "features")
    private List<BaseFeature<T>> baseFeatures;

    @JSONField(serialize=false)
    public List<T> getListBaseGisEntity(){
        List<T> baseGisEntityList=new ArrayList<T>();
        for (BaseFeature baseFeature :this.baseFeatures) {
            baseFeature.getProperties().setGeojson(baseFeature.getGeometry().toString());
            baseGisEntityList.add((T) baseFeature.getProperties());
        }
        return baseGisEntityList;
    }

}
